package tan.dft.gps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import tan.dft.listviewlotrinh.Lotrinh;
import android.graphics.Color;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class Showlotrinh extends FragmentActivity implements LocationListener {

	private GoogleMap googleMap;
	private ArrayList<LatLng> points;
	private Geocoder geocoder;
	private List<Address> addresses;
	private ArrayList<Lotrinh> array;
	private String add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotrinh);
		points = new ArrayList<LatLng>();
		SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map);
		googleMap = fm.getMap();

		if (googleMap != null) {
			googleMap.setMyLocationEnabled(true);
			LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
			Criteria criteria = new Criteria();
			String provider = locationManager.getBestProvider(criteria, true);
			Location location = locationManager.getLastKnownLocation(provider);
			if (location != null) {
				onLocationChanged(location);
			}
			locationManager.requestLocationUpdates(provider, 20000, 0, this);

			googleMap.setOnMapClickListener(new OnMapClickListener() {
				@Override
				public void onMapClick(LatLng point) {
					MarkerOptions markerOptions = new MarkerOptions();
					markerOptions.position(point);
					markerOptions.title("Position");
					markerOptions.snippet("Latitude:" + point.latitude + ","
							+ "Longitude:" + point.longitude);
					PolylineOptions polylineOptions = new PolylineOptions();
					polylineOptions.color(Color.GREEN);
					polylineOptions.width(5);
					points.add(point);
					polylineOptions.addAll(points);
					googleMap.addPolyline(polylineOptions);
					googleMap.addMarker(markerOptions);
				}
			});

			googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
				@Override
				public void onMapLongClick(LatLng point) {
					googleMap.clear();
					points.clear();
				}
			});

		}

		array = getIntent().getParcelableArrayListExtra("mylist");
		if (array != null) {
			for (int i = 0; i < array.size() - 1; i++) {

				Lotrinh src = array.get(i);
				Lotrinh dest = array.get(i + 1);
				PolylineOptions rectOptions = new PolylineOptions().add(
						new LatLng(src.getLat(), src.getLng())).add(
						new LatLng(dest.getLat(), dest.getLng()));

				geocoder = new Geocoder(this, Locale.getDefault());
				try {
					addresses = geocoder.getFromLocation(src.getLat(),
							src.getLng(), 1);
					String address = addresses.get(0).getAddressLine(0);
					String city = addresses.get(0).getAddressLine(1);
					String country = addresses.get(0).getAddressLine(2);
					add = address + " | " + city + " | " + country;
					googleMap.addMarker(new MarkerOptions()
							.position(new LatLng(src.getLat(), src.getLng()))
							.title(add).snippet("Hello"));

					if (i == array.size() - 2) {
						addresses = geocoder.getFromLocation(dest.getLat(),
								dest.getLng(), 1);
						String address1 = addresses.get(0).getAddressLine(0);
						String city1 = addresses.get(0).getAddressLine(1);
						String country1 = addresses.get(0).getAddressLine(2);
						add = address1 + " | " + city1 + " | " + country1;
						googleMap.addMarker(new MarkerOptions()
								.position(
										new LatLng((dest.getLat()), dest
												.getLng())).title(add)
								.snippet("Hello"));
					}

				} catch (IOException e) {
					e.printStackTrace();
				}

				Polyline polyline = googleMap.addPolyline(rectOptions);
				polyline.setColor(Color.GREEN);
			}

		}

	}

	public void onLocationChanged(Location location) {
		// Getting latitude of the current location
		double latitude = location.getLatitude();
		// Getting longitude of the current location
		double longitude = location.getLongitude();
		// Creating a LatLng object for the current location
		LatLng latLng = new LatLng(latitude, longitude);
		// Showing the current location in Google Map
		googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		// Zoom in the Google Map
		googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onProviderDisabled(String provider) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}
}
